package com.lc.qianrenshiye.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.qianrenshiye.R;
import com.lc.qianrenshiye.conn.OrderListPost;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.List;

/**
 * Created by Administrator on 2016/11/19.
 */
public class OrderAdapter extends ArrayAdapter<OrderListPost.OrderEntity> {

    private LayoutInflater layoutInflater;
    private Context context;
    private PayOrContinue payOrContinue;

    public OrderAdapter(Context context, List<OrderListPost.OrderEntity> lists, PayOrContinue payOrContinue) {
        super(context, 0, lists);

        this.context = context;

        layoutInflater = LayoutInflater.from(context);

        this.payOrContinue = payOrContinue;

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        OrderListPost.OrderEntity orderEntity = getItem(position);

        ViewHolder viewHolder;

        if (convertView == null) {

            convertView = BoundViewHelper.boundView(viewHolder = new ViewHolder(), ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) layoutInflater.inflate(R.layout.item_order_list, null)));

            convertView.setTag(viewHolder);
        } else {

            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.order_time_tv.setText(orderEntity.posttime);// "05-16-2016 16:00"
        viewHolder.order_table_num_tv.setText(orderEntity.seats);
        viewHolder.order_number_tv.setText(orderEntity.id);
        viewHolder.order_count_tv.setText("x" + orderEntity.num);
        viewHolder.order_total_price_tv.setText(orderEntity.amount);

        if (orderEntity.paystatus.equals("0")) {

            viewHolder.order_state_tv.setText("未支付");
            viewHolder.item_order_list_botton_rl.setVisibility(View.VISIBLE);
//            viewHolder.order_go_to_pay_tv.setEnabled(true);

        } else {

            viewHolder.order_state_tv.setText("已支付");
            viewHolder.item_order_list_botton_rl.setVisibility(View.GONE);
//            viewHolder.order_go_to_pay_tv.setEnabled(false);
        }



        viewHolder.order_go_to_pay_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                payOrContinue.pay(position);
            }
        });


        viewHolder.order_continue_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                payOrContinue.continueChoose(position);
            }
        });

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                payOrContinue.notPayOnItemclick(position);

            }
        });

        return convertView;
    }

    class ViewHolder {

        @BoundView(R.id.order_time_tv) private TextView order_time_tv;
        @BoundView(R.id.order_table_num_tv) private TextView order_table_num_tv;
        @BoundView(R.id.order_number_tv) private TextView order_number_tv;
        @BoundView(R.id.order_count_tv) private TextView order_count_tv;
        @BoundView(R.id.order_total_price_tv) private TextView order_total_price_tv;
        @BoundView(R.id.order_state_tv) private TextView order_state_tv;
        @BoundView(R.id.item_order_list_botton_rl) private RelativeLayout item_order_list_botton_rl;
        @BoundView(R.id.order_go_to_pay_tv) private TextView order_go_to_pay_tv;
        @BoundView(R.id.order_continue_tv) private TextView order_continue_tv;

    }

    public interface PayOrContinue {

        void pay(int pos);

        void continueChoose(int pos);

        void notPayOnItemclick (int pos);
    }

}
